package com.wsmhz.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.wsmhz.dto.User;
import com.wsmhz.security.core.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * create by tangbj on 2018/3/25
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @PostMapping("/register")
    @ResponseBody
    public void regist(User user, HttpServletRequest request) {

        //不管是注册用户还是绑定用户，都会拿到一个用户唯一标识。
        String userId = user.getUsername();
        //*********
        //  处理业务，注册用户还是绑定用户
        //******
        providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
        System.out.println("注册用户");
    }

    @JsonView(User.UserSimpleView.class)
    @GetMapping
    @ResponseBody
    public ServerResponse<List<User>> query(@Valid @RequestBody User user, BindingResult errors){
        List<User> users = new ArrayList<>();
        users.add(user);
        return  ServerResponse.createBySuccess(users);
    }

    @JsonView(User.UserDetailView.class)
    @GetMapping("/{id}")
    @ResponseBody
    public ServerResponse<User> getInfo(@PathVariable String id) {
        System.out.println("进入getInfo服务");
        User user = new User();
        user.setUsername("jim");
        user.setPassword("123456");
        return ServerResponse.createBySuccess(user);
    }
}
